package com.newly.center.sys.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.newly.api.sys.feign.RoleClient;
import com.newly.center.sys.service.role.api.IRoleService;
import com.newly.center.sys.service.tenant.api.ITenantService;
import com.newly.common.base.annotation.NewlyLog;
import com.newly.common.base.constants.AuthConstant;
import com.newly.common.base.constants.MenuConstant;
import com.newly.common.base.constants.ServerUriConstant;
import com.newly.common.base.entity.PageUtils;
import com.newly.common.base.entity.R;
import com.newly.common.base.entity.auth.ICurrentTenant;
import com.newly.common.base.entity.sys.auth.role.bo.RoleTypeBo;
import com.newly.common.base.entity.sys.auth.role.dto.RoleDto;
import com.newly.common.base.entity.sys.auth.role.po.RolePo;
import com.newly.common.base.entity.sys.auth.role.vo.RoleTypeVo;
import com.newly.common.base.entity.sys.auth.role.vo.RoleVo;
import com.newly.common.base.entity.sys.tenant.bo.SimpleTenantBo;
import com.newly.common.base.entity.sys.tenant.vo.SimpleTenantVo;
import com.newly.common.core.utils.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author gjm guan
 * @date 2021/11/1 15:16
 * @desc
 */
@RestController
@RequestMapping(ServerUriConstant.NEWLY_SYS_ROLE)
public class RoleApi implements RoleClient {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private RequestUtils requestUtils;

    @Autowired
    private ITenantService tenantService;

    @NewlyLog(ability = MenuConstant.Ability.ADD, title = "角色", isOpen = true)
    @Override
    public R create(RoleDto roleDto) {
        boolean result = false;
        try {
            Integer roleId = roleService.save(roleDto);
            result = roleId != null;
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(result);
    }

    @NewlyLog(ability = MenuConstant.Ability.UPD, title = "角色", isOpen = true)
    @Override
    public R change(RoleDto roleDto) {
        boolean result = false;
        try {
            result = roleService.modifyById(roleDto);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(result);
    }

    @NewlyLog(ability = MenuConstant.Ability.DEL, title = "角色", isOpen = true)
    @Override
    public R deleteByIds(List<Integer> roleIds, Integer must) {
        boolean result = false;
        try {
            result = roleService.removeByIds(roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(result);
    }

    @NewlyLog(ability = MenuConstant.Ability.QUE, title = "角色", isOpen = false)
    @Override
    public R list(Integer offset, Integer limit, String createBy, String fromTime, String toTime, String keyword) {
        PageUtils pageUtils = null;
        try {
            pageUtils = roleService.list(offset, limit, createBy, fromTime, toTime, keyword);
            List<RoleVo> roleVos = null;
            if (!ObjectUtil.isEmpty(pageUtils) && !ObjectUtil.isEmpty(pageUtils.getList())) {
                List<RolePo> rolePos = pageUtils.getList();
                roleVos = BeanUtil.copyToList(rolePos, RoleVo.class);
                for (RoleVo roleVo : roleVos) {
                    List<SimpleTenantVo> simpleTenantVos = null;
                    List<SimpleTenantBo> simpleTenantBos = roleService.getBindUserOfRole(roleVo.getId().toString());
                    if (!ObjectUtil.isEmpty(simpleTenantBos)) {
                        simpleTenantVos = BeanUtil.copyToList(simpleTenantBos, SimpleTenantVo.class);
                    } else {
                        simpleTenantVos = new ArrayList<>(0);
                    }
                    roleVo.setBindTenants(simpleTenantVos);
                }
            }
            pageUtils.setList(roleVos);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(pageUtils);
    }

    @Override
    public R queryInAuth() {
        List<RoleVo> roleVos = null;
        try {
            List<RolePo> rolePos = roleService.getInAuth();
            if (!ObjectUtil.isEmpty(rolePos)) {
                roleVos = BeanUtil.copyToList(rolePos, RoleVo.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(roleVos);
    }

    @Override
    public R queryTypes() {
        List<RoleTypeVo> roleTypeVos = null;
        try {
            List<RoleTypeBo> roleTypeBos = roleService.getTypes();
            if (!ObjectUtil.isEmpty(roleTypeBos)) {
                roleTypeVos = BeanUtil.copyToList(roleTypeBos, RoleTypeVo.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(roleTypeVos);
    }

    @Override
    public R queryEnable() {
        AuthConstant.Enable[] values = null;
        try {
            values = AuthConstant.Enable.values();
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(values);
    }

    @Override
    public R queryCreateTenant() {
        List<SimpleTenantVo> simpleTenantVos = null;
        try {
            List<SimpleTenantBo> simpleTenantBos = roleService.getCreateTenant();
            if (!ObjectUtil.isEmpty(simpleTenantBos)) {
                simpleTenantVos = BeanUtil.copyToList(simpleTenantBos, SimpleTenantVo.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(simpleTenantVos);
    }

    @Override
    public R queryById(Integer roleId) {
        ICurrentTenant currentTenant = requestUtils.getCurrentTenant();
        currentTenant.assertIncludeRole(roleId);
        RoleVo roleVo = null;
        try {
            RolePo rolePo = roleService.getById(roleId);
            if (!ObjectUtil.isEmpty(rolePo)) {
                roleVo = BeanUtil.copyProperties(rolePo, RoleVo.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(roleVo);
    }

    @Override
    public R queryBindUserOfRole(String roleId) {
        List<SimpleTenantVo> simpleTenantVos = null;
        try {
            List<SimpleTenantBo> simpleTenantBos = roleService.getBindUserOfRole(roleId);
            if (!ObjectUtil.isEmpty(simpleTenantBos)) {
                simpleTenantVos = BeanUtil.copyToList(simpleTenantBos, SimpleTenantVo.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(simpleTenantVos);
    }

    @Override
    public R bind(String tenantId, String roleId) {
        boolean result = false;
        try {
            result = roleService.tenantBindRole(tenantId, roleId);
        } catch (Exception e) {
            e.printStackTrace();
            return R.ok(e.getMessage());
        }
        return R.ok(result);
    }
}
